package impl;

import dao.User;
import dao.UserDAO;
import jdbc.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDAO {
    @Override
    public void register(User user) {
        try (
             Connection connection = DBUtil.getConnection()
        ){
            String sql = "insert into user(username,password,nickname,age) values(?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getNickname());
            preparedStatement.setInt(4,user.getAge());
            int count = preparedStatement.executeUpdate();
            System.out.println(count>0?"注册成功":"注册失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User login(String username, String password) {
        User user = new User();
        try (
             Connection connection = DBUtil.getConnection()
        ){
            String sql = "select * from user where username = ? and password = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                System.out.println("登录成功");
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));

            }else {
                System.out.println("登录失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void upDate(User user) {
        try (
                Connection conn = DBUtil.getConnection()
        ){
            String sql = "update user set password = ?,nickname = ? where username = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,user.getPassword());
            pst.setString(2,user.getNickname());
            pst.setString(3,user.getUsername());
            int count = pst.executeUpdate();
            System.out.println(count>0?"修改成功":"修改失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int delete(int id) {
        try (
                Connection conn = DBUtil.getConnection()
        ){
            String sql = "delete from user where id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,id);
            int count = pst.executeUpdate();
            System.out.println(count>0?"删除成功":"删除失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public User queryById(int id) {
        User user = null;
        try (
                Connection conn = DBUtil.getConnection()
        ){
            String sql = "select * from user where id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,id);
            ResultSet rs = pst.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  user;
    }

    @Override
    public User queryByNameAndPassword(String username, String password) {
        User user = null;
        try (
                Connection conn = DBUtil.getConnection()
        ){
            String sql = "select * from user where username = ? and password = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,username);
            pst.setString(2,password);
            ResultSet rs = pst.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> queryAll() {
        List<User> List = new ArrayList<>();
        User user = null;
        try (
                Connection conn = DBUtil.getConnection()
        ){
            String sql = "select * from user";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                List.add(user);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return List;
    }
}
